package com.example.myproject.fragment.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myproject.R;

/**
 * Created by Administrator on 2018/3/27.
 */

public class HomePagerAdapter extends PagerAdapter {

    private int[] imagResource = new int[]{
            R.drawable.timg_1024_615, R.drawable.timg_page2, R.drawable.timg_page3,
            R.drawable.timg_page4, R.drawable.timg_page5, R.drawable.timg_page6,
            R.drawable.timg_page7, R.drawable.timg_page8, R.drawable.timg_page9,
    };
    private ImageView[] ivArr = new ImageView[9];
    private Context context;

    public HomePagerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        for (int i = 0; i < 9; i++) {
            ivArr[i] = new ImageView(context);
            ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
            lp.gravity = Gravity.CENTER;
            ivArr[i].setLayoutParams(lp);
            ivArr[i].setImageResource(imagResource[i]);
        }
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        int poi = position % 9;
        container.removeView(ivArr[poi]);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int poi = position % 9;
        container.addView(ivArr[poi]);
        return ivArr[poi];
    }
}
